$(function () {
    var click = false;      // 用于记录开始抽奖按钮的点击状态
    // 抽奖规则弹框显示与隐藏
    $(".prize_content .rule").click(function(){
        if (click == false) {
            $(".rule_alert").css("display","block");
            $(".mask").css("display","block");
        }
    });
    $(".prize_content .myPrize").click(function(){
        window.location.href = 'prizelist.html'
    });
    $(".rule_alert .know").click(function(){
        $(".rule_alert").css("display","none");
        $(".mask").css("display","none");
    });
    $(".rule_alert>p img").click(function(){
        $(".rule_alert").css("display","none");
        $(".mask").css("display","none");
    });
    // 确认抽奖弹框显示与隐藏
    $(".prize_content .start_btn").click(function(){
        if (!click) {
            $(".remind_alert").css("display","block");
            $(".mask").css("display","block");
        }
    });
    $(".remind_alert .pp1 img").click(function(){
        $(".remind_alert").css("display","none");
        $(".mask").css("display","none");
    });
    $(".result_remind_alert .pp1 img").click(function(){
        $(".result_remind_alert").css("display","none");
        $(".mask").css("display","none");
    });
    // 条纹背景倾斜运动
    var speed = 40;         //数字越大速度越慢
    var tab = document.getElementById("demo");
    var tab1 = document.getElementById("demo1");
    var tab2 = document.getElementById("demo2");
    tab2.innerHTML = tab1.innerHTML;
    function Marquee() {
        if(tab2.offsetWidth - tab.scrollLeft <= 0){
            tab.scrollLeft -= tab1.offsetWidth
        }else{
            tab.scrollLeft++;
        }
    }
    if (!click) {
        MyMar = setInterval(Marquee, speed);
    }else{
        window.clearInterval(MyMar);
    }

    // 加载奖品信息
    loadPrize();
    function loadPrize(){
        $.ajax({
            url:"http://a.91jfk.com/Product/Prize/index?platform=1",
            type:"get",
            dataType:"json",
            success:function(res){
                console.log(res);
                // 500集分币
                $('.luck-unit-0').find('.text').html(res[1].pname.substring(0,res[1].pname.length - 3) + '</br>集分币');
                $('.luck-unit-7').find('.text').html(res[1].pname.substring(0,res[1].pname.length - 3) + '</br>集分币');
                $('.luck-unit-9').find('.text').html(res[1].pname.substring(0,res[1].pname.length - 3) + '</br>集分币');
                // 1000集分币
                $('.luck-unit-3').find('.text').html(res[0].pname.substring(0,res[1].pname.length - 2) + '</br>集分币');
                $('.luck-unit-6').find('.text').html(res[0].pname.substring(0,res[1].pname.length - 2) + '</br>集分币');
                $('.luck-unit-12').find('.text').html(res[0].pname.substring(0,res[1].pname.length - 2) + '</br>集分币');
                // 优乐美
                $('.luck-unit-5').find('.text').text(res[4].pname);
                $('.luck-unit-15').find('.text').text(res[4].pname);
                $('.luck-unit-11').find('.text').text(res[4].pname);
                // 充电宝
                $('.luck-unit-2').find('.text').text(res[2].pname);
                $('.luck-unit-10').find('.text').text(res[2].pname);
                // 手机
                $('.luck-unit-4').find('.text').text(res[3].pname);
                $('.luck-unit-14').find('.text').text(res[3].pname);
            },
            error:function(err){
                console.log(err);
            }
        });
    }
    var position;           // 记录中奖位置
    function setPosition(a){
        return position = a;
    }
    // 设置抽奖的属性和方法
    var luck = {
        index: -1,      //当前转动到哪个位置，起点位置
        count: 0,       //总共有多少个位置
        timer: 0,       //setTimeout的ID，用clearTimeout清除
        speed: 20,      //初始转动速度
        times: 0,       //转动次数
        cycle: 50,      //转动基本次数：即至少需要转动多少次再进入抽奖环节
        prize: -1,      //中奖位置
        init: function(id) {
            if($("#" + id).find(".luck-unit").length > 0) {
                $luck = $("#" + id);
                $units = $luck.find(".luck-unit");
                this.obj = $luck;
                this.count = $units.length;
                console.log(this);      //this指代的是luck对象
                $luck.find(".luck-unit-" + this.index).addClass("active");
            };
        },
        roll: function() {
            var index = this.index;
            var count = this.count;
            var luck = this.obj;
            $(luck).find(".luck-unit-" + index).removeClass("active");
            index += 1;
            if(index > count - 1) {
                index = 0;
            };
            $(luck).find(".luck-unit-" + index).addClass("active");
            this.index = index;
            return false;
        },
        stop: function(index) {
            this.prize = index;
            return false;
        },
        random: function(a,b){      // 随机数函数，控制概率
            var sum = 0,
                factor = 0,
                random = Math.random();
            for(var i = b.length - 1; i >= 0; i--) {
                sum += b[i];        // 统计概率总和
            }
            random *= sum;          // 生成概率随机数
            for(var i = b.length - 1; i >= 0; i--) {
                factor += b[i];
                if(random <= factor){
                    return a[i];
                }
            }
            return null;
        }
    };
    function rotate(position) {
        var a = position;
        luck.times += 1;
        luck.roll();
        if(luck.times > luck.cycle + 10 && luck.prize == luck.index) {
            clearTimeout(luck.timer);
            setTimeout(function(){
                var text = $(".luck-unit-" + luck.prize).eq(0).find('.text').text();
                console.log(text.substring(-3));
                if (text == '谢谢') {
                    $('.mask').show();
                    $('.result_remind_alert').show().find('.pp2').text('继续努力...');
                    $('.result_remind_alert').find('.sure').show().click(function(){
                        $('.mask').hide();
                        $('.result_remind_alert').hide();
                        $(this).hide();
                    });
                } else if(text.substr(-3) == '集分币'){
                    $('.mask').show();
                    $('.result_remind_alert').show().find('.pp2').text('恭喜你抽中'+text+'已存入你的账户余额...');
                    $('.result_remind_alert').find('.sure').show().click(function(){
                        $('.mask').hide();
                        $('.result_remind_alert').hide();
                        $(this).hide();
                    });
                } else {
                    $('.mask').show();
                    switch(text){
                        case '优乐美':
                            $('.result_remind_alert').show().find('.pp2').text('恭喜您抽中:'+text+'一杯,可在我的奖品中查看');
                            break;
                        case '充电宝':
                            $('.result_remind_alert').show().find('.pp2').text('恭喜您抽中:'+text+'一个,可在我的奖品中查看');
                            break;
                        default:
                            $('.result_remind_alert').show().find('.pp2').text('恭喜您抽中:'+text+'一部,可在我的奖品中查看');
                            break;
                    }
                    $('.result_remind_alert').find('.receive').show().click(function(){
                        $('.mask').hide();
                        $('.result_remind_alert').hide();
                        $(this).hide();
                        window.location.href = 'adress_list.html?id=' + localStorage.luck_id;
                    });
                }
                luck.prize = -1;
                luck.times = 0;
                click = false;
            },1000);
        } else {
            if(luck.times < luck.cycle) {
                luck.speed -= 10;
            } else if(luck.times == luck.cycle) {
                //抽奖活动概率算法，不同奖项的获取概率不同，按概率去随机生成对应的奖品
                /*var arr1 = ['50集分币','100集分币','150集分币','200集分币','300集分币','500集分币','谢谢参与'];   //所有抽奖商品数组，需要根据后台数据更改
                var arr2 = ['7','0,2,3,6,9,10,14','12','5,15','4','11','1,13,8'];   //所有商品在抽奖转盘上对应的位置的下标，需要根据后台数据更改
                var arr3 = [0.07,0.15,0.2,0.15,0.08,0.05,0.3];      //所有商品中奖的概率，需要根据后台数据更改
                var res = luck.random(arr2,arr3).split(",");        //字符串转数组
                console.log(res);
                var len = res.length;  
                var ran = Math.random() * (len) | 0;
                console.log(ran);
                var index = res[ran];
                console.log(index);*/
                luck.prize;
            } else {
                if(luck.times > luck.cycle + 10 && ((luck.prize == 0 && luck.index == 7) || luck.prize == luck.index + 1)) {
                    luck.speed += 110;
                } else {
                    luck.speed += 20;
                }
            }
            if(luck.speed < 40) {
                luck.speed = 40;
            };
            luck.timer = setTimeout(rotate, luck.speed);
        }
        return false;
    };

    // 点击继续按钮开始抽奖
    start();
    function start() {
        luck.init('luck');
        $(".remind_alert .sure").click(function() {
            $(".remind_alert").css("display","none");
            $(".mask").css("display","none");
            if (click) {
                return false;
            } else {
                //向后端接口发请求返回中奖结果
                $.ajax({　
                    url: "http://a.91jfk.com/Product/Prize/go_prize",
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        localStorage.setItem('luck_id',data.id);
                        if (data.code == '1') {
                            luck.speed = 100;
                            console.log(data.msg);                            
                            var arr1 = ['500集分币','1000集分币','手机','充电宝','优乐美','谢谢参与'];            // 所有奖品
                            var arr2 = [[0,7,9],[3,6,12],[4,14],[2,10],[5,11,15],[1,13,8]];                       // 每个奖品对应的位置
                            switch(data.msg){
                                case "未中奖":
                                    var len = arr2[5].length; 
                                    luck.prize = arr2[5][Math.random() * (len) | 0];
                                    setPosition(arr2[5][Math.random() * (len) | 0]);
                                    break;
                                case "华为荣耀 note9":
                                    var len = arr2[2].length; 
                                    luck.prize = arr2[2][Math.random() * (len) | 0];
                                    setPosition(arr2[2][Math.random() * (len) | 0]);
                                    break;
                                case "优乐美":
                                    var len = arr2[4].length; 
                                    luck.prize = arr2[4][Math.random() * (len) | 0];
                                    setPosition(arr2[4][Math.random() * (len) | 0]);
                                    break;
                                case "充电宝":
                                    var len = arr2[3].length; 
                                    luck.prize = arr2[3][Math.random() * (len) | 0];
                                    setPosition(arr2[3][Math.random() * (len) | 0]);
                                    break;
                                case "500集分币":
                                    var len = arr2[0].length; 
                                    luck.prize = arr2[0][Math.random() * (len) | 0];
                                    setPosition(arr2[0][Math.random() * (len) | 0]);
                                    break;
                                case "1000集分币":
                                    var len = arr2[1].length; 
                                    luck.prize = arr2[1][Math.random() * (len) | 0];
                                    setPosition(arr2[1][Math.random() * (len) | 0]);
                                    break;
                            }
                            console.log(luck.prize);
                            rotate();
                            click = true;
                            return false;
                        }
                    },
                    error:function(err){
                        console.log(err);
                    }
                });
                //ajax结束    
            }
        });
    };
});